package com.songhuan.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songhuan.web.dto.*;
import com.songhuan.web.dto.query.*;
import com.songhuan.web.entity.*;
import com.songhuan.web.mapper.*;
import com.songhuan.web.service.*;
import com.songhuan.web.tools.dto.*;
import com.songhuan.web.dto.AppUserDto;
import com.songhuan.web.dto.LeaveRecordDto;
import com.songhuan.web.dto.OrganizationDto;
import com.songhuan.web.dto.query.LeaveRecordPagedInput;
import com.songhuan.web.entity.AppUser;
import com.songhuan.web.entity.Enums;
import com.songhuan.web.entity.LeaveRecord;
import com.songhuan.web.entity.Organization;
import com.songhuan.web.mapper.AppUserMapper;
import com.songhuan.web.mapper.LeaveRecordMapper;
import com.songhuan.web.mapper.OrganizationMapper;
import com.songhuan.web.service.LeaveRecordService;
import com.songhuan.web.tools.Extension;
import com.songhuan.web.tools.dto.PagedResult;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import lombok.SneakyThrows;
import com.songhuan.web.tools.*;
/**
 * 请假记录功能实现类
 */
@Service
public class LeaveRecordServiceImpl extends ServiceImpl<LeaveRecordMapper, LeaveRecord> implements LeaveRecordService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的LeaveRecord表mapper对象
     */
    @Autowired
    private LeaveRecordMapper _LeaveRecordMpper;

 			
			
 			
    @Autowired
    private OrganizationMapper _OrganizationMapper;
			
 			
			

    /**
     * 请假记录分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<LeaveRecordDto> List(LeaveRecordPagedInput input) {

      
        //声明一个支持请假记录查询的(拉姆达)表达式
        LambdaQueryWrapper<LeaveRecord> queryWrapper = Wrappers.<LeaveRecord>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,LeaveRecord::getId,input.getId())
                .eq(input.getCreatorId()!=null,LeaveRecord::getCreatorId,input.getCreatorId());
   //如果前端搜索传入查询条件则拼接查询条件
        if(input.getUserId()!=null) {
            queryWrapper=queryWrapper.eq(LeaveRecord::getUserId,input.getUserId());
       	 }
        if(input.getOrganizationId()!=null) {
            queryWrapper=queryWrapper.eq(LeaveRecord::getOrganizationId,input.getOrganizationId());
       	 }

        if(Extension.isNotNullOrEmpty(input.getReason())) {
            queryWrapper=queryWrapper.eq(LeaveRecord::getReason,input.getReason());
       	 }
        if(input.getAuditUserId()!=null) {
            queryWrapper=queryWrapper.eq(LeaveRecord::getAuditUserId,input.getAuditUserId());
       	 }
        if(input.getAuditStatus()!=null) {
            queryWrapper=queryWrapper.eq(LeaveRecord::getAuditStatus,input.getAuditStatus());
       	 }

        if(Extension.isNotNullOrEmpty(input.getReply())) {
            queryWrapper=queryWrapper.eq(LeaveRecord::getReply,input.getReply());
       	 }
        if(input.getBeginTimeRange()!=null&&input.getBeginTimeRange().stream().count()>1)
        {
            queryWrapper=queryWrapper.lt(LeaveRecord::getBeginTime,input.getBeginTimeRange().get(1));
            queryWrapper=queryWrapper.gt(LeaveRecord::getBeginTime,input.getBeginTimeRange().get(0));
        }
        if(input.getEndTimeRange()!=null&&input.getEndTimeRange().stream().count()>1)
        {
            queryWrapper=queryWrapper.lt(LeaveRecord::getEndTime,input.getEndTimeRange().get(1));
            queryWrapper=queryWrapper.gt(LeaveRecord::getEndTime,input.getEndTimeRange().get(0));
        }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(LeaveRecord::getCreationTime);
        //构建一个分页查询的model
        Page<LeaveRecord> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取请假记录数据
        IPage<LeaveRecord> pageRecords= _LeaveRecordMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _LeaveRecordMpper.selectCount(queryWrapper);

        //把LeaveRecord实体转换成LeaveRecord传输模型
        List<LeaveRecordDto> items= Extension.copyBeanList(pageRecords.getRecords(),LeaveRecordDto.class);
        for (LeaveRecordDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto CreatorAppUserDTO=new AppUserDto();
            AppUser CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto AuditUserDTO = new AppUserDto();        
            AppUser  AuditUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getAuditUserId()));
           if(AuditUserEntity!=null) {  
          BeanUtils.copyProperties(AuditUserDTO,AuditUserEntity);
            item.setAuditUserDto(AuditUserDTO); 
           }
          
           
          	            
           //查询出关联的Organization表信息
           OrganizationDto OrganizationDTO = new OrganizationDto();
            Organization OrganizationEntity= _OrganizationMapper.selectOne(Wrappers.<Organization>lambdaQuery().eq(Organization::getId,item.getOrganizationId()));
           if(OrganizationEntity!=null) {  
          BeanUtils.copyProperties(OrganizationDTO,OrganizationEntity);
            item.setOrganizationDto(OrganizationDTO); 
           }
          
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto UserDTO = new AppUserDto();        
            AppUser  UserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getUserId()));
           if(UserEntity!=null) {  
          BeanUtils.copyProperties(UserDTO,UserEntity);
            item.setUserDto(UserDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个请假记录查询
     */
    @SneakyThrows
    @Override
    public LeaveRecordDto Get(LeaveRecordPagedInput input) {
       if(input.getId()==null)
        {
            return new LeaveRecordDto();
        }
        PagedResult<LeaveRecordDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new LeaveRecordDto();
    }

    /**
     *请假记录创建或者修改
     */
    @SneakyThrows
    @Override
    public LeaveRecordDto CreateOrEdit(LeaveRecordDto input) {

        //声明一个请假记录实体
        LeaveRecord LeaveRecord = new LeaveRecord();

        //把前端传入的input参数拷贝到请假记录实体
        BeanUtils.copyProperties(LeaveRecord,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(LeaveRecord);

        //定义一个返回给前端的请假记录传输模型
        LeaveRecordDto LeaveRecordDto=new LeaveRecordDto();

        //同理把操作的请假记录实体拷贝给请假记录传输模型
        BeanUtils.copyProperties(LeaveRecordDto,LeaveRecord);

        //把传输模型返回给前端
        return LeaveRecordDto;
    }

    /**
     *请假批准
     */
    @SneakyThrows
    @Override
    public void ApplySuccess(LeaveRecordDto input) {

        input.setAuditStatus(Enums.AuditStatus.审核通过.index());
        input.setReply("请假审核批准");
        //声明一个请假记录实体
        LeaveRecord LeaveRecord = new LeaveRecord();

        //把前端传入的input参数拷贝到请假记录实体
        BeanUtils.copyProperties(LeaveRecord,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(LeaveRecord);

        //定义一个返回给前端的请假记录传输模型

    }


}
